﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LAVAA.BL.Control;
using LAVAA.DAL;
using LAVAA.WEB.Models;

namespace LAVAA.WEB.Controllers
{
    public class UseCase01Controller : Controller
    {

        public ActionResult ListActivities()
        {
            return View(new LAVAA.BL.Control.UserCase01().FindActivities(User.Identity.Name));
        }

        //
        // GET: /UseCase01/Create

        public ActionResult CreateActivity()
        {
            return View(new LAVAA.WEB.Models.ActivityView());
        }

        //
        // POST: /UseCase01/Create

        [HttpPost]
        public ActionResult CreateActivity(LAVAA.WEB.Models.ActivityView activityView)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    UserCase01 userStory = new UserCase01();
                    var user = userStory.GetUser(User.Identity.Name);
                    userStory.AddActivity(new LAVAA.DAL.Activity()
                    {
                        Date = DateTime.Now,
                        DurationInMinutes = activityView.Activity.DurationInMinutes,
                        Comment = activityView.Activity.Comment,
                    }, 
                    activityView.SelectedActivityType, user.Id);
                    return RedirectToAction("ListActivities");
                }
                return View(activityView);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return View(activityView);
            }
        }


        //
        // GET: /UseCase01/Edit/5

        public ActionResult EditActivity(Guid id)
        {
            UserCase01 userStory = new UserCase01();
            ActivityView activityView = new LAVAA.WEB.Models.ActivityView();
            var activity = userStory.FindActivityById(id);
            if (activity != null)
            {
                activityView.Activity = activity;
                activityView.SelectedActivityType = activity.ActivityType.Id;
            }
            return View(activityView);
        }

        //
        // POST: /UseCase01/Edit/5

        [HttpPost]
        public ActionResult EditActivity(LAVAA.WEB.Models.ActivityView view)
        {
            try
            {
                UserCase01 userStory = new UserCase01();
                userStory.UpdateActivity(view.Activity);
                return RedirectToAction("/ListActivities/");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /UseCase01/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /UseCase01/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
